import { GateSessionKeyComponent } from "../../../../../Model/Server/Demo/Gate/GateSessionKeyComponent";
import { ExpandOf } from "../../../../../../Decorator/Decorator";
import { TimerComponent } from "../../../../../../Core/Module/Timer/TimerComponent";

/**
 * 使用 Expand 扩展方法
 * 
 * 必须作出以下申明，才能调用对象的扩展方法
 */
declare module "../../../../../Model/Server/Demo/Gate/GateSessionKeyComponent" {

    interface GateSessionKeyComponent {
        Add(key: number, account: string): void;
        Get(key: number): string;
        Remove(key: number): void;
        TimeoutRemoveKey(key: number): Promise<void>;
    }

}

@ExpandOf(GateSessionKeyComponent)
export class GateSessionKeyComponentSystem {

    private static Add(self: GateSessionKeyComponent, key: number, account: string): void {
        self.sessionKey.Add(key, account);
        self.TimeoutRemoveKey(key).then();
    }

    private static Get(self: GateSessionKeyComponent, key: number): string {
        let account = self.sessionKey.TryGetValue(key);
        return account;
    }

    private static Remove(self: GateSessionKeyComponent, key: number): void {
        self.sessionKey.Remove(key);
    }

    private static async TimeoutRemoveKey(self: GateSessionKeyComponent, key: number): Promise<void> {
        await TimerComponent.Instance.WaitAsync(20000);
        self.sessionKey.Remove(key);
    }

}